<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<?php
require('simple_html_dom_helper.php');

function __advance_get_html($url) {

    // is cURL installed yet?
    if (!function_exists('curl_init')) {
        die('Sorry cURL is not installed!');
    }

    $_return = '';

    // OK cool - then let's create a new cURL resource handle
    $ch = curl_init();

    // Set URL to download
    curl_setopt($ch, CURLOPT_URL, $url);

    // User agent
    curl_setopt($ch, CURLOPT_USERAGENT, 'nhadat.net Crawler 1.0');

    // Include header in result? (0 = yes, 1 = no)
    curl_setopt($ch, CURLOPT_HEADER, 0);

    // Timeout in seconds
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);

    // Should cURL return or print out the data? (true = return, false = print)
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $_return = curl_exec($ch);
    curl_close($ch);

    return $_return;
}

$page_url = 'http://www.tapchidiaoc.net/gl/tin-dia-oc/dia-oc-trong-nuoc/2011/09/thi-truong-dat-gia-re-ven-tphcm-lay-ha-tang-tren-giay-de-quang-cao-ban-dat.html';

$html = __advance_get_html($page_url);
$dom = str_get_dom($html);
$title = '';
$head = '';
$content = '';

$title_dom = $dom->find('div#articeContent h1.titleArtice', 0);

if (method_exists($title_dom, 'innertext')) {
    $title = $title_dom->innertext();
}

unset($title_dom);

$head_dom = $dom->find(htmlspecialchars_decode('div#detail-content strong[0]'), 0);
if (method_exists($head_dom, 'innertext'))
    $head = $head_dom->innertext();
unset($head_dom);


$content_dom = $dom->find('div#detail-content', 0);
if (method_exists($content_dom, 'innertext')) {
    $content.= $content_dom->innertext();
}
unset($content_dom);

//remove content
$stTemp = $content;
$arrRemoveRegular = explode("\n", 'xpath:div#detail-content strong[0]');
foreach ($arrRemoveRegular as $rRegular) {
    if (substr($rRegular, 0, 6) == 'xpath:') {//remove kieu xpath
        $rRegular = str_replace('xpath:', '', $rRegular);
        if ($rRegular == '')
            continue;

        $domTemp = $dom->find(htmlspecialchars_decode($rRegular), 0);

        if (method_exists($domTemp, 'outertext'))
            $stTemp = str_replace($domTemp->outertext, '', $stTemp);

        unset($domTemp);
    }
    
    if ($stTemp != ''){
        $content = $stTemp;
    }
    
    unset($stTemp);
}


//remove some content
//if ($title != '' && $content != '') {
//    
//    $domTemp = $dom->find('div#detail-content div[62]',0);
//    
//    if (method_exists($domTemp, 'outertext'))
//        $content = str_replace($domTemp->outertext, '', $content);
//
//    unset($domTemp);
//}


$image = ''; //Đường dẫn hình đầu tiên trong bài viết - lấy dể làm thumb

$row->url_prefix = 'http://www.dothi.net';

if ($row->url_prefix != '') {//pre fix
    //fix image location
    $arr = array();
    preg_match_all("/\<img.*?src\s*=\s*\"([^\"]*)\"[^\>]*\>/", $content, $arr);
    if (isset($arr[1])) {
        $arr1 = array();
        $arr2 = array();
        foreach ($arr[1] as $v) {

            if (strtolower(substr($v, 0, 4)) == 'http')
                continue;
            $arr1[] = $v;
            if (!strpos($v, '/'))
                $v = $row->page_url . $v; //trường hợp image chỉ có tên hình
            $arr2[] = $row->url_prefix . $v;
        }
        $content = str_replace($arr1, $arr2, $content);
    }

    //fix link location
    $arr = array();
    preg_match_all("/<a href([\s]*)=([\s]*)\"([^\"]*)\"(.*)>(.*)<\/a>/i", $content, $arr);
    if (isset($arr[1])) {
        $arr1 = array();
        $arr2 = array();
        foreach ($arr[3] as $v) {
            if (strtolower(substr($v, 0, 4)) == 'http')
                continue;
            $arr1[] = $v;
            $arr2[] = $row->url_prefix . $v;
        }
        $content = str_replace($arr1, $arr2, $content);
    }
}

//Lấy ảnh đầu tiên
$arr = array();
preg_match_all("/\<img.*?src\s*=\s*\"([^\"]*)\"[^\>]*\>/", $content, $arr);

$image = (isset($arr[1][0])) ? $arr[1][0] : '';

if ($image == '' && $row->thumb != '')
    $image = $row->thumb; //ko có thì lấy thumb

if (strpos($image, '/') === false)
    $image = $row->page_url . $image;
if (strtolower(substr($image, 0, 4)) != 'http')
    $image = $row->url_prefix . $image;

$msg.='IMG: ' . $image . '<br>';

//luu anh
//if ($image != '')
//    $image = $this->_save_image($image);
//html_entity_decode trường hợp chữ bị mã kiểu: X&#226;y nh&#224; ri&#234;ng tr&#234;n 250m2 t&#7841;i
$title = htmlspecialchars(html_entity_decode($title, ENT_QUOTES, 'UTF-8'));
$content = html_entity_decode($content, ENT_QUOTES, 'UTF-8');


echo "title:<br/> ";
echo $title;
echo "<br/>";

echo "header:<br/> ";
echo $head;
echo "<br/>";

echo "content:<br/> ";
echo $content;
echo "<br/>";

echo '<img src="' . $image . '" />';